package cn.car91.activity;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;
import cn.car91.R;
import cn.car91.bean.Goods;

import com.lidroid.xutils.view.annotation.ViewInject;

public class GoodsDetailActivity extends BaseActivity {

	@ViewInject(R.id.goods_pic)
	ImageView goodsImage;
	@ViewInject(R.id.goods_create_address)
	TextView goodsCreateAddress;
	@ViewInject(R.id.goods_price)
	TextView goodsPrice;
	@ViewInject(R.id.goods_firm_code)
	TextView goodsFirmCode;
	@ViewInject(R.id.goods_firm_name)
	TextView goodsFirmName;
	@ViewInject(R.id.goods_status)
	TextView goodsStatus;
	
	private Goods goods;
	private Bitmap bitmap;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_goods_detail);
		super.onCreate(savedInstanceState);
		
		((TextView) findViewById(R.id.header_text)).setText("商品基础信息");
		
		goods = (Goods) getIntent().getSerializableExtra("goods");
		if(goods!=null){
			initData(goods);
		}
	}

	private void initData(Goods goods){
		//goodsImage.setImageBitmap(getHttpBitmap(goods.getTitleSrc()));
		new Task().execute(); 
		goodsPrice.setText(String.valueOf(goods.getPrice())+"元");
		goodsCreateAddress.setText(goods.getPlace());
		goodsFirmCode.setText(goods.getFaccode());
		goodsFirmName.setText(goods.getFac_name());
		goodsStatus.setText(goods.getFac_status());
	}
	
	/** 
     * 获取网落图片资源  
     * @param url 
     * @return 
     */  
    public Bitmap getHttpBitmap(String url){  
        URL myFileURL;  
        Bitmap bitmap=null;  
        try{  
            myFileURL = new URL(url);  
            //获得连接  
            HttpURLConnection conn=(HttpURLConnection)myFileURL.openConnection();  
            //设置超时时间为6000毫秒，conn.setConnectionTiem(0);表示没有时间限制  
            conn.setConnectTimeout(6000);  
            //连接设置获得数据流  
            conn.setDoInput(true);  
            //不使用缓存  
            conn.setUseCaches(false);  
            //这句可有可无，没有影响  
            conn.connect();  
            //得到数据流  
            InputStream is = conn.getInputStream();  
            //解析得到图片  
            bitmap = BitmapFactory.decodeStream(is);  
            //关闭数据流  
            is.close();  
        }catch(Exception e){  
            e.printStackTrace();  
        }  
          
        return bitmap;  
    }  
    
    class Task  extends AsyncTask<String, Integer, String>{  
    	
        @Override  
        protected String doInBackground(String... params) {  
            //得到可用的图片  
            bitmap = getHttpBitmap(goods.getTitleSrc());  
            return null;  
        }  
  
        @Override  
        protected void onPostExecute(String result) {  
            // TODO Auto-generated method stub  
            super.onPostExecute(result);  
             Message msg=new Message();  
             msg.what=0;  
             handler.sendMessage(msg);  
  
        }  
          
    }  
    Handler handler=new Handler(){  
  
        @Override  
        public void handleMessage(Message msg) {  
            // TODO Auto-generated method stub  
            super.handleMessage(msg);  
            switch(msg.what){  
            case 0:  
            	goodsImage.setImageBitmap(bitmap);  
            }  
        }  
          
    };  
}
